<template>
    <el-form ref="ruleFormRef" :rules="rules" :model="formBanner" label-width="80px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="轮播图名称" prop="bannerName">
        <el-input v-model="formBanner.bannerName" placeholder="请输入轮播图名称"/>
      </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="轮播图封面" prop="bannerImg">
            <!--上传图片控件 start-->
            <el-upload v-model:file-list="fileList" :action="uploadURL" name="fileResource" list-type="picture-card" :auto-upload="true"
                       :limit="1" :on-success="handleSuccess">
              <el-icon><Plus /></el-icon>
            </el-upload>
            <!--上传图片控件 end-->
          </el-form-item>
        </el-col> 
      </el-row>
    </el-form>
      <div class="dialog-button-wrap">
        <el-button @click="close">取消</el-button>
        <el-button @click="editBanner(ruleFormRef)" type="success" :loading="subLoading" color="#083a6d">保存</el-button>
      </div>
    </template>
    
    <script setup lang="ts">
    import { ref,reactive,onMounted,shallowRef } from 'vue'
    import {ElMessage,UploadFile,UploadProps,UploadUserFile,FormInstance,FormRules} from 'element-plus'
    import {editBannerApi} from "@/api/edu/banner/banner"
    import '@wangeditor/editor/dist/css/style.css' // 引入 css

    // 定义表单实例对象
    const ruleFormRef = ref<FormInstance>()
    // 表单约束规则对象
    const rules = reactive<FormRules>({
      bannerImg: [{required: true,message: '封面不能为空',trigger: 'blur'}],
      bannerName: [{ required: true,message: '轮播图名称不能为空',trigger: 'blur' }],

    })
    // 定义服务器路径
    const url = import.meta.env.VITE_APP_BASE_API
    // 图片上传到服务器的路径
    const uploadURL = url+'user/userIcon'
    
    
    // 定义事件
    const emit = defineEmits(['closeEditBannerForm','success'])
    
    // 按钮状态
    const subLoading = ref(false)
    // 表单数据对象
    const formBanner = reactive({
      id: 0,
      bannerImg: '',
      bannerName: '',

    })
    // 获取父组件传过来的bannerInfo对象
    const props = defineProps(['bannerInfo'])
    const bannerInfo = ref(props.bannerInfo)
    // 给表单填充数据
    for (const key in formBanner) {
      formBanner[key] = bannerInfo.value[key]
    }
    
    // 处理回显封面
    const fileList = ref<UploadUserFile[]>([
        {
          name: '封面图',
          url: url+'uploadFile/'+formBanner.bannerImg
        }
    ])
    
    // 更新轮播图信息
    const editBanner = async (formEl: FormInstance |undefined)=> {
      if(!formEl)return
      await formEl.validate(async (valid,fields)=> {
        subLoading.value = true
        if(valid){
          const { data } = await editBannerApi(formBanner)
          if(data.status === 200){
            ElMessage.success(data.message)
            emit('success')
          }else {
            ElMessage.error(data.message)
          }
        }else {
          ElMessage.error('提交失败，你还有未填写的项！')
        }
      })
      subLoading.value = false
    }

  
    // 取消表单
    const close = ()=>{
      emit('closeEditBannerForm')
    }
    </script>
    
    <style scoped>
    .dialog-button-wrap {
      text-align: center;
      margin-top: 20px;
    }
    </style>
    